<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      class="tab-control"
      ref="tabControl1"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @pullingUp="loadMore"
      @scroll="contentScroll"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <goods-list :goods="goods[currentType].list" />
    </scroll>
    <back-top @click.native="bakClick()" v-show="isShowBackTop" />
  </div>
</template>

<script>
import HomeSwiper from "./childComps/HomeSwiper.vue";
import RecommendView from "./childComps/RecommendView.vue";
import FeatureView from "./childComps/FeatureView.vue";

import NavBar from "components/common/navbar/NavBar.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import BackTop from "components/content/backTop/BackTop.vue";

import { getHomeMultidata, getHomeGoods } from "network/home";

// import {request} from"network/request"
import axios from "axios";
export default {
  name: "Home-name",
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  created() {
    //1.请求多个数据
    this.getHomeMultidata();

    // 2.请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  updated() {
    // console.log(this.goods[this.currentType].list);
  },
  mounted() {
    //1.图片加载完成的事件监听
    const refresh = this.debounce(this.$refs.scroll.refresh, 500);
    this.bus.$on("itmeImageLoad", () => {
      refresh();
    });
  },
  //home访问切换后回到原位
  // activated() {
  //   this.$refs.scroll.scrollTo(0,this.saveY,10)
  //   this.$refs.scroll.refresh()
  // },
  // deactivated() {
  //   console.log(this.saveY);
  //   this.saveY=this.$refs.scroll.scroll.y
  // },
  methods: {
    // 事件监听相关的方法
    //节流函数
    // throttle(func, wait, immediate) {
    //   var timeout;
    //   return function () {
    //     let context = this;
    //     let args = arguments;
    //     let later = function () {
    //       timeout = null;
    //       if (!immediate) func.apply(context, args);
    //     };
    //     let callNow = immediate && !timeout;
    //     clearTimeout(timeout);
    //     timeout = setTimeout(later, wait);
    //     if (callNow) func.apply(context, args);
    //   };
    // },
    //防抖函数
    debounce(func, delay) {
      let timer = null;
      return function (...args) {
        //...args写三个.表示可以传多个参数
        if (timer) {
          //如果存在timer，就清除掉
          clearTimeout(timer);
        }
        timer = setTimeout(() => {
          func.apply(this, args); //apply改变this指向args
        }, delay);
      };
    },
    //tab组件点击
    tabClick(index) {
      // 接收TabControl子组件传过来的下标index
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    //点击返回顶部
    bakClick() {
      this.$refs.scroll.scrollTo(0, 0, 300);
      console.log("返回顶部");
    },
    //向下滚动显示backTop
    contentScroll(position) {
      //1.判断backTop是否显示
      this.isShowBackTop = -position.y > 628;
      //2.决定tabControl是否吸顶(position:fixed)
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    //上拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    //监听轮播图加载完毕后，再获取tab组件的offsetTop值
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    // 网络请求相关的方法
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.data.banner.list;
        this.recommends = res.data.data.recommend.list;
      });
    },
    // getHomeGoods(type) {
    //   const page = this.goods[type].page + 1;
    //   getHomeGoods(type, page).then((res) => {
    //     //将数据保存进goods数组 1.ES6结构方法处理:
    //     this.goods[type].list.push(...res.data.data.list);
    //     //2.for循环处理
    //     // for (let i of res.data.data.list) {
    //     //   this.goods[type].list.push(i)
    //     // }
    //     this.goods[type].page += 1;
    //     this.$refs.scroll.finishPullUp()
    //     console.log(res);
    //   });
    // },
    //不封装请求数据
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      axios({
        url: "http://152.136.185.210:7878/api/hy66/home/data",
        params: {
          type: type,
          page: page,
        },
      }).then((res) => {
        // console.log(res);
        console.log(res);
        //将接口数据存储到goods中
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;

        //完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
}
.home-nav {
  background: var(--color-tint);
  color: #fff;

  /* position: fixed;
  right: 0px;
  left: 0px;
  top: 0px;
  z-index: 9; */
}
.tab-control {
  position: relative;
  z-index: 9;
}
.content {
  position: absolute;
  right: 0;
  left: 0;
  top: 44px;
  bottom: 49px;
  /* height: calc(100% - 54px); */
  overflow: hidden;
}
</style>